﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;

namespace RBS.Common
{

    /// <summary>
    /// 传值方式
    /// </summary>
    public enum DataType
    {
        text,//Url传值
        json,//Form传值
    }

    /// <summary>
    /// 分页控件属性
    /// </summary>
    public class Pager
    {
        /// <summary>
        /// 分页加载的模板名称
        /// </summary>
        public string PagerTempName { get; set; }

        /// <summary>
        /// 分页控件ID
        /// </summary>
        public string PagerId { get; set; }

        /// <summary>
        /// 是否显示分页控件
        /// </summary>
        public bool PagerShow { get; set; }

        /// <summary>
        /// 数据类型
        /// </summary>
        public DataType DataType { get; set; }

        /// <summary>
        /// 每页显示的记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurPage { get; set; }

        /// <summary>
        /// 显示页码的数目
        /// </summary>
        public int PageNum { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalSize { get; set; }

        /// <summary>
        /// 控制器名称
        /// </summary>
        public string ControllerName { get; set; }

        /// <summary>
        /// 动作名称
        /// </summary>
        public string ActionName { get; set; }

        /// <summary>
        /// 页码列表
        /// </summary>
        public List<PageModel> PageList { get; set; }

        /// <summary>
        /// 参数列表
        /// </summary>
        private Dictionary<string, string> Parameters { get; set; }

        public string PagerData { get; set; }
    }

    /// <summary>
    /// 页码属性
    /// </summary>
    public class PageModel
    {

        public PageModel() { }

        public PageModel(int pageIndex, string pageText)
        {
            PageIndex = pageIndex;
            PageText = pageText;
        }

        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 文本
        /// </summary>
        public string PageText { get; set; }
    }

    /// <summary>
    /// 分页帮助类
    /// </summary>
    public static class PageHelper
    {

        public static MvcHtmlString Pager(this HtmlHelper helper,
            string pagerId, //分页控件Id
            int curPage, //当前页码
            int totalSize, //总记录数
            string pagerTemp = "_PagerTemp", //分页控件模板
            int pageSize = 10, //每页显示10条
            int pageNum = 5, //显示的页码数目
            string datatype = "text", //默认是Url传值
            Dictionary<string, string> parameters = null //参数列表
        )
        {
            System.Web.Routing.RouteData routeData = helper.ViewContext.RouteData;
            string controllerName = routeData.GetRequiredString("controller");//当前的Controller
            string actionName = routeData.GetRequiredString("action");//当前的action
            DataType type = DataType.text;
            bool flag = Enum.TryParse(datatype, out type);
            if (!flag)
            {
                type = DataType.text;
            }
            return Pager(helper, pagerId, curPage, pageSize, totalSize, pageNum, controllerName, actionName, type, parameters);
        }

        public static MvcHtmlString Pager(this HtmlHelper helper,
            string pagerId, //分页控件Id
            int curPage, //当前页
            int pageSize, //每页显示的记录数目
            int totalSize, //总记录
            int pageNum, //显示的页码数目
            string controllerName, //控制器名称
            string actionName, //动作名称
            DataType datatype, //默认是Url传值
            Dictionary<string, string> parameters //参数列表
        )
        {
            Pager pager = new Pager();
            pager.PagerTempName = "_PagerTemp";
            pager.PagerId = pagerId;
            pager.PageSize = pageSize;
            pager.TotalSize = totalSize;
            pager.CurPage = curPage;
            pager.TotalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize) : (totalSize / pageSize) + 1;
            pager.PageNum = pageNum;
            pager.ControllerName = controllerName;
            pager.ActionName = actionName;
            pager.DataType = datatype;
            if (datatype == DataType.text)//Url传值
            {
                pager.PagerData = UrlGetParameter(parameters);
                if (pager.TotalPage > 1 && pager.TotalPage >= curPage)
                {
                    pager.PagerShow = true;//显示分页
                    List<PageModel> pageList = new List<PageModel>();
                    int step = pageNum;//偏移量
                    int leftPageNum = (curPage - pageNum < 1) ? 1 : (curPage - pageNum);//左边界
                    int rightPageNum = (curPage + pageNum > pager.TotalPage) ? pager.TotalPage : (curPage + pageNum);//右边界
                    int pageCount = rightPageNum - leftPageNum + 1;
                    var sourceList = Enumerable.Range(leftPageNum, pageCount);
                    pageList.AddRange(sourceList.Select(p => new PageModel
                    {
                        PageIndex = p,
                        PageText = p.ToString()
                    }));
                    pager.PageList = pageList;
                }
                else
                {
                    pager.PagerShow = false;//页数少于一页，则不显示分页
                }
            }
            else
            {
                pager.PagerData = UrlJsonParameter(parameters);
                if (pager.TotalPage > 1 && pager.TotalPage >= curPage)
                {
                    pager.PagerShow = true;//显示分页
                    List<PageModel> pageList = new List<PageModel>();
                    int step = pageNum;//偏移量
                    int leftPageNum = (curPage - pageNum < 1) ? 1 : (curPage - pageNum);//左边界
                    int rightPageNum = (curPage + pageNum > pager.TotalPage) ? pager.TotalPage : (curPage + pageNum);//右边界
                    int pageCount = rightPageNum - leftPageNum + 1;
                    var sourceList = Enumerable.Range(leftPageNum, pageCount);
                    pageList.AddRange(sourceList.Select(p => new PageModel
                    {
                        PageIndex = p,
                        PageText = p.ToString()
                    }));
                    pager.PageList = pageList;
                }
                else
                {
                    pager.PagerShow = false;//页数少于一页，则不显示分页
                }
            }
            return helper.Partial(pager.PagerTempName, pager);
        }


        #region 参数设置

        /// <summary>
        /// Url数据
        /// </summary>
        private static string UrlGetParameter(Dictionary<string, string> parameters)
        {
            if (null != parameters && parameters.Count > 0)
            {
                StringBuilder paramStr = new StringBuilder();
                foreach (KeyValuePair<string, string> item in parameters)
                {
                    paramStr.Append("&" + item.Key.ToLower() + "=" + item.Value);
                }
                return paramStr.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Json数据
        /// </summary>
        private static string UrlJsonParameter(Dictionary<string, string> parameters)
        {
            if (null != parameters && parameters.Count > 0)
            {
                StringBuilder paramStr = new StringBuilder();
                int i = 1;
                paramStr.Append("{");
                foreach (KeyValuePair<string, string> item in parameters)
                {
                    if (i < parameters.Count)
                    {
                        paramStr.Append(string.Format("{0}:{1}", item.Key.ToLower(), item.Value));
                        paramStr.Append(",");
                    }
                    else
                    {
                        paramStr.Append(string.Format("{0}:{1}", item.Key.ToLower(), item.Value));
                    }
                    i++;
                }
                paramStr.Append("}");
                return paramStr.ToString();
            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}